package com.zxy.leetcode._00000_00099._00060_00069;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/plus-one/
 *
 * 加一
 * 1 <= digits.length <= 100
 * 0 <= digits[i] <= 9
 */
public class Test00066 {

    public static void main(String[] args) {
        Test00066 test = new Test00066();
        int[] nums1 = {1, 2, 3};
        System.out.println(Arrays.toString(test.plusOne(nums1)));

        int[] nums2 = {9, 9, 9};
        System.out.println(Arrays.toString(test.plusOne(nums2)));
    }

    public int[] plusOne(int[] digits) {
        // 特殊情况，全部是9
        boolean flag9 = true;
        for (int digit : digits) {
            if (digit != 9) {
                flag9 = false;
                break;
            }
        }
        if (flag9) {
            int len = digits.length+1;
            int[] nums = new int[len];
            nums[0] = 1;
            return nums;
        }

        int c = 1;
        for (int i=digits.length-1; i>=0; i--) {
            int sum = digits[i] + c;
            if (sum >= 10) {
                digits[i] = sum - 10;
                c = 1;
            }else {
                digits[i] = sum;
                c = 0;
            }
        }

        return digits;
    }
}
